<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\Admin;
use App\Models\Role;

class DepartmentController extends BaseController
{
    
    /**
     * 部门列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function departList() 
    {

        $data = Department::orderBy('displayorder','asc')->get()->toArray();

        return self::json($data);
    }

    /**
     * 单个部门数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function departOne(Request $request) 
    {

         $id     = intval($request->input('id'));

        if ($id == 0)       return self::error('id有误');

        $depart = Department::find($id);

        if(empty($depart))  return self::error('id有误');

        return self::json($depart);
    }

    /**
     * 部门添加
     * @return [type] [description]
     */
    public function departAdd(Request $request) 
    {

        $params = [];

        $params['name']    = $request->input('name');
        $params['roleids'] = $request->input('roleids');

        if ($params['name'] == null || $params['roleids'] == null) {
            return self::error('必填参数不能为空');
        }

        $request->input('status')       == null || $params['status']       =  $request->input('status');
        $request->input('displayorder') == null || $params['displayorder'] =  $request->input('displayorder');

        $depart = new Department();
        $depart->setAttributes($params);

        $result = $depart->save();
        
        return $result ? self::success('添加成功') : self::error('添加失败');
    }

    /**
     * 部门编辑
     * @return [type] [description]
     */
    public function departEdit(Request $request) 
    {

        $params = [];
        $request->input('name')         == null || $params['name']         = $request->input('name');
        $request->input('roleids')      == null || $params['roleids']      = $request->input('roleids');
        //$request->input('status')       == null || $params['status']       =  $request->input('status');
        $request->input('displayorder') == null || $params['displayorder'] =  $request->input('displayorder');

        $id     = intval($request->input('id'));

        if ($id == 0)       return self::error('id有误');

        $depart = Department::find($id);

        if(empty($depart))  return self::error('id有误');

        if (empty($params)) return self::success('编辑成功');

        $depart = Department::find($id);

        $depart->setAttributes($params);

        $result = $depart->save();
        
        return $result ? self::success('编辑成功') : self::error('编辑失败');
    }


    /**
     * 部门状态编辑
     * @return [type] [description]
     */
    public function departStatusEdit(Request $request) 
    {

        $status =  $request->input('status');

        $id     = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $depart = Department::find($id);


        if(empty($depart)) {

            return self::error('id有误');
        }

        //如果停用 部门检测 部门下的管理员
        if ($status == Department::STATUS_STOP) {

             $admin = Admin::where('department', $id)->where('status', Admin::STATUS_BEGIN)->get()->toArray();

            if (count($admin)) {
                return self::error('部门下无启用管理员方可停用');
            }
        }

        $depart = new Department();
        $depart->status  = $status;

        $result = $depart->save();
        
        return $result ? self::success('状态更改成功') : self::error('状态更改失败');
    }

    /**
     * 部门删除
     * @return [type] [description]
     */
    public function departDelete(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $depart = Department::find($id);


        if(empty($depart)) {

            return self::error('id有误');
        }
        //检测 部门下面是否有 管理员， 如果有管理员 并且不为 停用状态的 不可删除
        $admin = Admin::where('department', $id)->where('status', Admin::STATUS_BEGIN)->get()->toArray();

        if (count($admin)) {
            return self::error('部门下无启用管理员方可删除');
        }

        $depart->isdelete = Admin::IS_DELETE;

        $result = $depart->save();
        
        return $result ? self::success('删除成功') : self::error('编辑失败');
    }

    /**
     * 部门角色查看
     * @return [type] [description]
     */
    public function departRoleLook(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $depart = Department::find($id);


        if(empty($depart)) {

            return self::error('id有误');
        }


        //根据ids字符串获得对应的角色
        $roles = Role::getRoleObj($depart->roleids);

        $rolesArr = [];

        foreach ($roles as $key => $val) {

            $rolesArr[$key]['name'] = $val->name; 
            $rolesArr[$key]['id']   = $val->id;

        }

        return self::json($rolesArr);
    }

    /**
     * 部门角色列表
     * @return [type] [description]
     */
    public function departRoleList(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) {

            return self::error('id有误');
        }

        $depart = Department::find($id);


        if(empty($depart)) {

            return self::error('id有误');
        }

        //部门角色数组
        $departRole = explode(',', $depart->roleids);
        //获得所有的角色
        $roles = Role::where('status',Role::STATUS_BEGIN)->get();

        $rolesArr = [];

        foreach ($roles as $key => $val) {

            $rolesArr[$key]['name']  = $val->name; 
            $rolesArr[$key]['id']    = $val->id;
            $rolesArr[$key]['check'] = in_array($val->id, $departRole) ?true : false;
        }

        return self::json($rolesArr);
    }
    /**
     * 部门角色编辑
     * @return [type] [description]
     */
    public function departRoleEdit(Request $request) 
    {

        $id      = intval($request->input('id'));
        $roleids = $request->input('roleids');

        if ($id == 0) {

            return self::error('id有误');
        }

        $depart = Department::find($id);


        if(empty($depart)) {

            return self::error('id有误');
        }

        $depart->roleids = $roleids;

        $result = $depart->save();

        return $result ? self::success('部门角色编辑成功') : self::error('部门角色编辑失败');
    }

 }
